import 'package:get/get.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/tools_base/base_list_controller.dart';
import 'package:xvideos/tools_base/global_store/store.dart';

class SettingAvatarLogic extends BaseListController {
  @override
  double get spaceH => 20;
  @override
  double get spaceV => 20;

  @override
  int get crossCount => 4;

  int selectIndex = -1;
  late final int rawIndex;

  @override
  fetchPageData({bool isRefresh = true, bool needLoading = false}) async {
    final res = await MineService.getPortrait();
    dataSource.addAll(res);
    isLoading = false;
    selectIndex = dataSource.indexOf(globalStore.meInfo?.portrait);
    rawIndex = selectIndex;
    update();
  }

  selectAavatar(int index) {
    selectIndex = index;
    update();
  }

  confirmChangeAvatar() async {
    if (globalStore.meInfo?.urrPortraitStatus == 1) {
      showToast('头像审核中～');
      return;
    }
    if (selectIndex == rawIndex || selectIndex == -1) {
      showToast('你都没选择，保存什么～');
      return;
    }
    final res = await MineService.updateUserInfo({'portrait': dataSource[selectIndex], 'isDefaultSource': true});
    if (res) {
      globalStore.updateUserInfo();
      showToast('更新成功');
      Get.back(result: true);
    }
  }
}
